#!/bin/bash

# 检查规则：
# 表的行数,
# username、device_id、event_id字段的空值个数,
# action_time字段的最大值、最小值
# 检查结果应该生成如下结构的json去汇报给质量管理平台（数据平台）
json_template='
{
  "target_table":"ods.user_action_log",
  "target_date":"2024-04-17",
  "rules":[
      {
        "rule_name":"表行数检查",
        "check_result":line_cnt_@@
      },
      {
        "rule_name":"字段空值检查",
        "target_field":"username",
        "check_result":username_null_@@
      },
      {
        "rule_name":"字段空值检查",
        "target_field":"device_id",
        "check_result":device_id_null_@@
      },
      {
        "rule_name":"字段空值检查",
        "target_field":"event_id",
        "check_result":event_id_null_@@
      },
      {
        "rule_name":"字段MAX检查",
        "target_field":"action_time",
        "check_result":action_time_max_@@
      },
      {
              "rule_name":"字段MIN检查",
              "target_field":"action_time",
              "check_result":action_time_min_@@
      }
  ]
}
'


cur_day=$(date +%Y-%m-%d)
pre_day=$(date -d'-1 day' +%Y-%m-%d)


sql="

select
count(1) as cnt,
sum(case when username is null then 1 else 0 end) as username_null_cnt,
sum(case when device_id is null then 1 else 0 end) as device_id_null_cnt,
sum(case when event_id is null then 1 else 0 end) as event_id_null_cnt,
max(action_time) as action_time_max,
min(action_time) as action_time_min

from ods.user_action_log
where dt='${pre_day}'
;

"

# 用beeline执行sql
res=$(beeline -u jdbc:hive2://doitedu:10000 -n root --outputformat=csv2 --showHeader=false -e "$sql")


# 8,0,8,0,1711036408000,1711036401000
echo $res

# 切割字符串，取值
line_cnt=$(echo $res | cut -d',' -f 1)
username_null_cnt=$(echo $res | cut -d',' -f 2)
device_id_null_cnt=$(echo $res | cut -d',' -f 3)
event_id_null_cnt=$(echo $res | cut -d',' -f 4)
action_time_max=$(echo $res | cut -d',' -f 5)
action_time_min=$(echo $res | cut -d',' -f 6)


# 把值替换到json模板中
json_str=$(echo $json_template | sed "s/line_cnt_@@/$line_cnt/g" | sed "s/username_null_@@/$username_null_cnt/g" | sed "s/device_id_null_@@/$device_id_null_cnt/g" | sed "s/event_id_null_@@/$event_id_null_cnt/g" | sed "s/action_time_max_@@/$action_time_max/g" | sed "s/action_time_min_@@/$action_time_min/g")

# 上报到数据平台(质量管理系统)
echo $json_str

res=$(curl -X POST -H "Content-Type: application/json" -H "Accept:application/json" -d "$json_str" http://192.168.77.2:8080/dq/ods_table_dq)


if [ $? -eq 0 ]
then

echo '任务执行成功'
exit 0

else
echo '任务执行失败'
exit 1

fi




















